﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNdotnet.Ezsocio.Core.Repository;
using CNdotnet.Ezsocio.DataEntities;
using CNdotnet.Ezsocio.DataProvider;

#endregion

namespace CNdotnet.Ezsocio.SQLServerProvider
{
    /// <summary>
    /// The repository of module
    /// </summary>
    public class ModuleRepository : RepositoryBase, IModuleRepository
    {
        /// <summary>
        /// Gets the modules.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public List<Module> GetModules(int userId)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var result = from u in db.GetTable<User>()
                        from m in db.GetTable<Module>()
                        from userModule in db.GetTable<UserModuleMap>()
                        where u.ID == userModule.UserId && userModule.ModuleId == m.ID && u.ID == userId
                        orderby m.ID ascending
                        select m;

                return result.ToList<Module>();
            }
        }
        
        /// <summary>
        /// Gets the specicle module by Id
        /// </summary>
        /// <param name="Id">The module Id</param>
        /// <returns></returns>
        public Module GetSingleById(int Id)
        {
            Module module = GetEntity<Module>(m => m.ID == Id);
            return module;
        }
    }
}
